package com.xusheng.cli.controller.back;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xusheng.cli.common.base.MyPage;
import com.xusheng.cli.common.base.Result;
import com.xusheng.cli.common.validation.Save;
import com.xusheng.cli.common.validation.Update;
import com.xusheng.cli.entity.downloadStatisticsModule.DownloadStatistics;
import com.xusheng.cli.service.DownloadStatisticsService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 下载统计表(DownloadStatistics)表控制层
 *
 * @author whr
 * @since 2024-06-24 15:21:29
 */
@Validated
@RestController
@RequestMapping("/api/v1/downloadStatistics")
public class DownloadStatisticsController {
    /**
     * 服务对象
     */
    @Resource
    private DownloadStatisticsService downloadStatisticsService;

    /**
     * 查询所有数据
     *
     * @param downloadStatistics 查询实体
     * @return 所有数据
     * @author whr
     * @since 2024-06-24 15:21:29
     */
    @GetMapping("queryAll")
    public Result<List<DownloadStatistics>> queryAll(DownloadStatistics downloadStatistics) {
        return Result.success(downloadStatisticsService.list(new QueryWrapper<>(downloadStatistics)));
    }

    /**
     * 分页查询所有数据
     *
     * @param page               分页对象
     * @param downloadStatistics 查询实体
     * @return 所有数据
     * @author whr
     * @since 2024-06-24 15:21:29
     */
    @GetMapping("queryByPage")
    public Result<MyPage<DownloadStatistics>> queryByPage(MyPage<DownloadStatistics> page, DownloadStatistics downloadStatistics) {
        DateTime start = null;
        DateTime end = null;
        if (StrUtil.isNotBlank(downloadStatistics.getStartTime()) && StrUtil.isNotBlank(downloadStatistics.getEndTime())) {
            start = DateUtil.beginOfDay(DateUtil.parse(downloadStatistics.getStartTime()));
            end = DateUtil.endOfDay(DateUtil.parse(downloadStatistics.getEndTime()));
        }
        return Result.success(downloadStatisticsService.page(page, new QueryWrapper<>(downloadStatistics)
                .ge(start != null, "create_time", start)
                .le(end != null, "create_time", end)));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     * @author whr
     * @since 2024-06-24 15:21:29
     */
    @GetMapping("queryOne/{id}")
    public Result<DownloadStatistics> selectOne(@PathVariable Serializable id) {
        return Result.success(downloadStatisticsService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param downloadStatistics 实体对象
     * @return 新增结果
     * @author whr
     * @since 2024-06-24 15:21:29
     */
    @PostMapping("insert")
    public Result<Object> insert(@Validated(Save.class) @RequestBody DownloadStatistics downloadStatistics) {
        return Result.isSuccess(downloadStatisticsService.save(downloadStatistics));
    }

    /**
     * 修改数据
     *
     * @param downloadStatistics 实体对象
     * @return 修改结果
     * @author whr
     * @since 2024-06-24 15:21:29
     */
    @PutMapping("update")
    public Result<Object> update(@Validated(Update.class) @RequestBody DownloadStatistics downloadStatistics) {
        return Result.isSuccess(downloadStatisticsService.updateById(downloadStatistics));
    }

    /**
     * 删除单条数据
     *
     * @param id 主键
     * @return 删除结果
     * @author whr
     * @since 2024-06-24 15:21:29
     */
    @DeleteMapping("delete/{id}")
    public Result<Object> delete(@PathVariable Long id) {
        DownloadStatistics downloadStatistics = new DownloadStatistics();
        downloadStatistics.setId(id);
        return Result.isSuccess(downloadStatisticsService.removeById(downloadStatistics));
    }

    /**
     * 批量删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     * @author whr
     * @since 2024-06-24 15:21:29
     */
    @DeleteMapping("batchDelete")
    public Result<Object> batchDelete(@Validated @Size(min = 1, message = "缺少删除所需的id") @RequestParam("idList") List<Long> idList) {
        return Result.isSuccess(downloadStatisticsService.batchDelete(idList, new DownloadStatistics()));
    }

    @GetMapping("getStatistics")
    public Result<Object> getStatistics(String startTime, String endTime) {
        Map<String, Object> map = new HashMap<>();
        DateTime start = null;
        DateTime end = null;
        if (StrUtil.isNotBlank(startTime) && StrUtil.isNotBlank(endTime)) {
            start = DateUtil.beginOfDay(DateUtil.parse(startTime));
            end = DateUtil.endOfDay(DateUtil.parse(endTime));
        }
        // 下载统计
        Long downloadCount = downloadStatisticsService.lambdaQuery()
                .eq(DownloadStatistics::getType, 0)
                .ge(start != null, DownloadStatistics::getCreateTime, start)
                .le(end != null, DownloadStatistics::getCreateTime, end)
                .count();
        map.put("downloadCount", downloadCount);
        // 下载区域统计
        List<Map<String, Integer>> addrDownloadCount = downloadStatisticsService.addrCount(0, start, end);
        map.put("addrDownloadCount", addrDownloadCount);
        // 注册区域统计
        List<Map<String, Integer>> addrRegisterCount = downloadStatisticsService.addrCount(1, start, end);
        map.put("addrRegisterCount", addrRegisterCount);
        // 日下载统计
        Long dayDownloadCount = downloadStatisticsService.lambdaQuery()
                .eq(DownloadStatistics::getType, 0)
                .ge(start != null, DownloadStatistics::getCreateTime, start)
                .le(end != null, DownloadStatistics::getCreateTime, end)
                .ge(start == null, DownloadStatistics::getCreateTime, DateUtil.beginOfDay(new Date()))
                .le(end == null, DownloadStatistics::getCreateTime, DateUtil.endOfDay(new Date()))
                .count();
        map.put("dayDownloadCount", dayDownloadCount);
        // 日注册统计
        Long dayRegisterCount = downloadStatisticsService.lambdaQuery()
                .eq(DownloadStatistics::getType, 1)
                .ge(start != null, DownloadStatistics::getCreateTime, start)
                .le(end != null, DownloadStatistics::getCreateTime, end)
                .ge(start == null, DownloadStatistics::getCreateTime, DateUtil.beginOfDay(new Date()))
                .le(end == null, DownloadStatistics::getCreateTime, DateUtil.endOfDay(new Date()))
                .count();
        map.put("dayRegisterCount", dayRegisterCount);
        // 域名展示统计
        List<Map<String, Integer>> hostShowCount = downloadStatisticsService.hostCount(2, start, end);
        map.put("hostShowCount", hostShowCount);
        return Result.success(map);
    }
}

